package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;

/**
 * User interface - main menu
 * The entry-point for the application:
 * Create an instance of class MainMenuUI and the constructor starts the application
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 *                    
 * (KBH): 2008.10.24: Change af manimenuStart to if
 */

public class LoanUI
{
  //the reference to the ControlLayer
  private LoanCtr lCtr;
//  private LoanContainer lc;
//  private DVDCtr dvdCtr;
//  private AddressCtr addrCtr;
//  private AddressBookUI addrUI;
//  private DvdUI dvdUI;
//  private DvdContainer dc;
  
   public LoanUI()
   {
     lCtr = new LoanCtr();     
   //  dvdCtr = new DVDCtr();
   //  addrCtr = new AddressCtr();
   //  addrUI = new AddressBookUI();
   //  dvdUI = new DvdUI();
   //  dc = DvdContainer.getInstance();
   //  lc = LoanContainer.getInstance();
   }
   
   public void start()
   {
       loanMenu();
   }
   
   public void loanMenu()
   {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            if(choise == 1)
            {
                createLoan();
            }
            else{
                if(choise == 2)
                {
                    listLoan();
                }
                else{
                    if(choise == 3)
                    {
                        removeLoan();
                    }
                    else{
                        if(choise == 4)
                        {
                            exit = true;
                        }
                    }
                }
        }//end while
    }//end start
}
    private int writeMainMenu()
    {       
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" *** LoanMenu ***");
        System.out.println(" (1) Loan Dvd");
        System.out.println(" (2) List Loans");
        System.out.println(" (3) Remove Loan");
        System.out.println(" (4) Return");
        System.out.print("\n Make your choise: ");
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return str Return the input from the keyboard 
     */
    private String inputtext(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        return str;
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return no Return the input from the keyboard
     */
    private long inputnumber(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        long no = Long.parseLong(str.trim());  // convert String 2 long
        return no;
    }

    /**
     * Create a loan on a person
     * @param
     * @return
     */
    public void createLoan()
    {
        long pid = inputnumber("Type in your Personal ID");
        lCtr.searchPersonDvd(pid);
        String serial = inputtext("Type in the Serial Number of the DVD you wish to Borrow");
        String dato = inputtext("Type in Todays Date");
        String period = inputtext("Type in how long you wish to borrow the DVD (in weeks)");
        String status = inputtext("Type in the Status for your reservation (Borrow/return)");
        System.out.println(" \n Your loan has now been created ");
        System.out.println(" \n The DVD you loaned is\n: ");
//        lCtr.searchDvd(dvdid); // need to find dvd id based on the serialnr
        lCtr.listEksemplarFromId(pid);
        System.out.println("\n Information about you \n");
        lCtr.searchPersonDvd(pid);
        System.out.println(" ");
        System.out.println(" Your receit ");
        System.out.println(" You have loaned a dvd with serial number:          "+serial);
        System.out.println(" The dvd shall be returned in:                      "+dato+ " Weeks");
        System.out.println(" The status is now:                                 "+status+ "\n");
        
    }

    /**
     * List the persons loan
     * @param
     * @return
     */
    public void listLoan()
    {
        long id = inputnumber("Type in your loaner ID");
        long id2 = inputnumber(" Type in your Personal ID");
        System.out.println(" \n Informations about your loan \n");
        lCtr.searchPersonDvd(id2);
        System.out.println("\n What you have loaned \n");
        lCtr.listEksemplarFromId(id);
    }
    
    /**
     * Removs the loan from the list
     * @param
     * @return
     */
    public void removeLoan()
    {
        String serienr = inputtext("Type the Serial Number of your DVD");
        long id = inputnumber("Type in Personal ID number");
        lCtr.listAll(id);        
        lCtr.removeCopy(serienr);
        System.out.print(" \n\n The copy with serial number \n\n"+serienr+ "\n\n has been removed \n\n");
        //System.out.print(" This is what is left on your loan ");
        //lCtr.listAll(id1);
        
    }
    
    
 }
